package com.lab.idea.bl.util;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.system.SystemUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 消息提示窗口
 * @author mao
 */
public class TipWindow {

    private static TipWindow TIP_WINDOW;

    private final JFrame frame;
    private final JEditorPane editorPane;
    private final boolean isWindows;

    private TipWindow() {
        frame = new JFrame();
        editorPane = new JEditorPane();
        editorPane.setEditable(false);
        editorPane.setContentType("text/html");
        frame.add(editorPane);
        //去掉窗口的装饰
        frame.setUndecorated(true);
        //设置窗体样式
        frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
        //窗体置顶
        frame.setAlwaysOnTop(true);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                hide();
            }
        });
        isWindows = SystemUtil.getOsInfo().isWindows();
    }

    static {
        try {
            UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
            TIP_WINDOW = new TipWindow();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void show(int width, int height,String message) {
        if (TIP_WINDOW == null || !TIP_WINDOW.isWindows) {
            return;
        }
        TIP_WINDOW.editorPane.setContentType("text/html");
        TIP_WINDOW.editorPane.setText(message);
        //获取当前焦点位置
//        Point location = MouseInfo.getPointerInfo().getLocation();
        // 获取工具
        Toolkit kit = Toolkit.getDefaultToolkit();
        //获取屏幕的尺寸
        Dimension screenSize = kit.getScreenSize();
        TIP_WINDOW.frame.setBounds((Convert.toInt(screenSize.getWidth()) - width)/2, (Convert.toInt(screenSize.height) - height * 2)/2, width, height);
        TIP_WINDOW.frame.setVisible(true);
    }

    public static void hide() {
        if (TIP_WINDOW == null) {
            return;
        }
        TIP_WINDOW.frame.setVisible(false);
    }

    public static void show(String msg) {
        String message = String.format("<div style=\"text-align: center\"><h1 style='color:red;'>BL-TOOL</h1><h2>%s</h2><h4>V 1.0</h4></div>",msg);
        TipWindow.show(300,120,message);
    }

    public static void show(String msg, long millis) {
        String message = String.format("<div style=\"text-align: center\"><h1 style='color:red;'>BL-TOOL</h1><h2>%s</h2><h4>V 1.0</h4></div>",msg);
        TipWindow.show(300,120,message);
        ThreadUtil.safeSleep(millis);
        TipWindow.hide();
    }

    public static void error(String msg, long millis) {
        String message = String.format("<div style=\"text-align: center\"><h1 style='color:red;'>BL-TOOL</h1><h2>%s</h2><h4>V 1.0</h4></div>",msg);
        TipWindow.show(400,300,message);
        ThreadUtil.safeSleep(millis);
        TipWindow.hide();
    }

    public static void main(String[] args) {
        TipWindow.show("启动中...");
        ThreadUtil.safeSleep(1000 * 5);
    }
}
